#!/bin/bash
##############################################################
source ./pkgutils.sh || exit 1
PKG_NAME=$(pkgname); if (( $? != 0)); then exit 2; fi
PKG_VERSION=$(pkgversion); if (( $? != 0)); then exit 3; fi
if [[ $(whoami) != "root" ]]; then
	echo "Please log in as user root...";
	exit 4;
fi
mkdir -pv ./tmp && cd ./tmp || exit 5;
##############################################################
# do setup tasks (location: /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp)

echo "setup of $PKG_NAME $PKG_VERSION";
# time zone
tar -xf ../tzdata2014g.tar.gz && \
ZONEINFO=/usr/share/zoneinfo && \
mkdir -pv $ZONEINFO/{posix,right} >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
for tz in etcetera southamerica northamerica europe africa antarctica asia australasia backward pacificnew systemv; do
    zic -L /dev/null   -d $ZONEINFO       -y "sh yearistype.sh" ${tz} && \
    zic -L /dev/null   -d $ZONEINFO/posix -y "sh yearistype.sh" ${tz} && \
    zic -L leapseconds -d $ZONEINFO/right -y "sh yearistype.sh" ${tz}
	if (( $? != 0 )); then
		errcho "Aborted while generating time zone information";
		exit 10;
	fi
done && \
cp -f zone.tab zone1970.tab iso3166.tab $ZONEINFO/ && \
find $ZONEINFO/ -type f >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
zic -d $ZONEINFO -p America/New_York && \
unset ZONEINFO && \
TZ_SELECT=$(tzselect) && \
cp -f /usr/share/zoneinfo/$TZ_SELECT /etc/localtime && \
echo "/etc/localtime" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files

if (($? != 0)); then
    errcho "Aborted while installing time zone information";
    exit 11;
fi

# adjusting tool chain, if using /tools
echo 'main() {}' > dummy.c && \
cc dummy.c -v -Wl,--verbose &> dummy.log && \
RELF="$(readelf -l a.out)";
if [[ $(echo "$RELF" | grep 'interpreter: /tools/lib') != "" ]]; then
	errcho "first stage tool linker in use... adjusting linker";
	mv -vf /tools/bin/{ld,ld-old} >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
	mv -vf /tools/$(gcc -dumpmachine)/bin/{ld,ld-old} >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
	mv -vf /tools/bin/{ld-new,ld} >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
	ln -sfv /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
	gcc -dumpspecs | sed -e 's@/tools@@g'                   \
    -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' >      \
    `dirname $(gcc --print-libgcc-file-name)`/specs && \
	echo "$(dirname $(gcc --print-libgcc-file-name))/specs" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
	cc dummy.c -v -Wl,--verbose &> dummy.log && \
	RELF="$(readelf -l a.out)";
	if (($? != 0)); then
		errcho "Aborted while adjusting tool chain";
		exit 12;
	fi
elif [[ $(echo "$RELF" | grep 'interpreter: /lib') != "" ]]; then
	errcho "linker library path is OK, nothing to do";
else
	errcho "Wrong readelf result:";
	errcho "$RELF";
	exit 13;
fi

# check in detail
errcho 'Check compiling log in detail... searching for "/usr/lib.*/crt[1in].*succeeded"';
CRTLNO=$(grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log | wc -l);
if (($CRTLNO < 3)); then
	errcho "$(grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log)";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 14;
fi;
errcho 'Check compiling log in detail... searching for "^ /usr/include"';
INCPATH="$(grep -B1 '^ /usr/include' dummy.log)";
if [[ $(echo "$INCPATH" | grep '^#include .* search starts here:') == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 15;
fi;
errcho 'Check compiling log in detail... searching for "SEARCH.*/usr/lib"';
SEARCHDIR="$(grep 'SEARCH.*/usr/lib' dummy.log | sed 's|; |\n|g')";
if (( $(echo "$SEARCHDIR" | grep '("/usr/lib.*")' | wc -l) < 1 || $(echo "$SEARCHDIR" | grep '("/lib.*")' | wc -l) < 1 )); then
	errcho "$SEARCHDIR";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 16;
fi;
errcho 'Check compiling log in detail... searching for "/lib.*/libc.so.6 succeeded"';
LIBCSO6="$(grep "/lib.*/libc.so.6 succeeded" dummy.log)";
if [[ $LIBCSO6 == "" ]]; then
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 17;
fi;
errcho 'Check compiling log in detail... searching for "ld-linux.*.so.2 at /lib.*/ld-linux.*.so.2"';
FOUND="$(grep 'ld-linux.*.so.2 at /lib.*/ld-linux.*.so.2' dummy.log)";
if [[ $FOUND == "" ]]; then
	errcho "$FOUND";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 8;
fi
rm -vf dummy.c a.out dummy.log



##############################################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package setup with code $ERR";
    exit 6;
fi
